home *** CD-ROM | disk | FTP | other *** search
- Path: inforamp.net!ts4-03
- From: rmorin@inforamp.net (Randy Charles Morin)
- Newsgroups: comp.lang.c++
- Subject: TDocManager problems
- Date: Tue, 26 Mar 96 08:10:32 GMT
- Organization: MiddleWorld SoftWare
- Message-ID: <4j88qk$q74@sam.inforamp.net>
- NNTP-Posting-Host: ts4-03.tor.inforamp.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- I had some problems with the behavior of OWL 2.5
- TDocManager. I encapsulated the changes.
-
- --------------------------------------------------------
- docman.h
- --------------------------------------------------------
- //{{TDocManager = MWDocManager}}
- class MWDocManager : public TDocManager {
- public:
- MWDocManager (int mode, TApplication* app);
- virtual ~MWDocManager ();
- protected:
- virtual int SelectDocPath(TDocTemplate** tpllist, int tplcount,
- char far* path, int buflen, long flags, bool
- save);
- protected:
- // TApplication * pApplication;
-
- //{{MWDocManagerVIRTUAL_BEGIN}}
- public:
- virtual TDocTemplate* SelectAnySave (TDocument& doc, bool samedoc = true);
- //{{MWDocManagerVIRTUAL_END}}
- }; //{{MWDocManager}}
- ----------------------------------------------------------
- docman.cpp
- ----------------------------------------------------------
- #ifndef MW_DOCMAN_H
- #include "docman.h"
- #endif
- #ifndef __DIR_H
- #include <dir.h>
- #endif
- #ifndef _DLGSH_INCLUDED_
- #include <dlgs.h>
- #endif
-
-
- //{{MWDocManager Implementation}}
-
-
- MWDocManager::MWDocManager (int mode, TApplication* app):
- TDocManager(mode, app)
- {
- // INSERT>> Your constructor code here.
-
- }
-
-
- MWDocManager::~MWDocManager ()
- {
- // INSERT>> Your destructor code here.
-
- }
-
- //
- // struct to pass data to the dialog hook function via OPENFILENAME.lCustData
- // under Win32s the OPENFILENAME passed to WM_INITDIALOG is a temporary copy
- //
- struct TplHookData { // pointer to this struct passed as lCustData
- long Flags; // original flags passed to SelectDocPath
- unsigned long far* DlgFlags; // pointer to dialog flags in OPENFILENAME
- char far* ExtBuf; // pointer to buffer holding the default extension
- TDocTemplate** TplList; // template pointer array
- int InitIndex; // starting index, ofn.nFilterIndex is inconsistent
- bool Save; // true if saving
- };
-
- //
- // dialog hook function to catch template selection changes
- //
- uint CALLBACK __export
- TplHook(HWND hDlg, uint msg, uint wParam, int32 lParam)
- {
- static TplHookData* hookData; // !!update to store in dialog property
- TDocTemplate** tpllist;
- OPENFILENAME* ofnHook;
- int idx;
- switch(msg) {
- case WM_COMMAND:
- #if defined(BI_PLAT_WIN32)
- if (LOWORD(wParam) != cmb1 || HIWORD(wParam) != CBN_SELCHANGE)
- #else
- if (wParam != cmb1 || HIWORD(lParam) != CBN_SELCHANGE)
- #endif
- return 0;
- idx = (int)::SendMessage((HWND)(uint)lParam, CB_GETCURSEL, 0, 0L);
- break;
-
- case WM_INITDIALOG:
- ofnHook = (OPENFILENAME*)lParam;
- hookData = (TplHookData*)ofnHook->lCustData;
- idx = hookData->InitIndex; // can't rely upon ofnHook->nFilterIndex
- break;
-
- default:
- return 0;
- }
- tpllist = hookData->TplList;
- long flags = tpllist[idx]->GetFlags() | hookData->Flags;
- if (hookData->Save)
- flags = (flags | dtNoReadOnly) & ~dtFileMustExist;
- *hookData->DlgFlags = flags & ~dtProhibited;
- if (tpllist[idx]->GetDefaultExt())
- strcpy(hookData->ExtBuf, tpllist[idx]->GetDefaultExt());
- else
- hookData->ExtBuf[0] = 0;
- ::SendDlgItemMessage(hDlg, chx1, BM_SETCHECK, ((flags&dtReadOnly) != 0), 0);
- ::ShowWindow(GetDlgItem(hDlg, chx1),
- (flags&dtHideReadOnly)?SW_HIDE:SW_SHOW);
- return 0; // flag as unprocessed
- }
-
- // prompts user with one or all templates to select file to open
- // returns the template index used for the selection (1-based), 0 if failure
- // this is Windows-specific, using the system-defined file open dialog box
- //
- int
- MWDocManager::SelectDocPath(TDocTemplate** tpllist, int tplcount,
- char far* path, int buflen, long flags, bool
- save)
- {
- char extbuf[MAXEXT-1]; // writable buffer for default file extension
- OPENFILENAME ofn; // local openfilename structure
- int index, count, len;
- char* filtbuf;
- char* pbuf;
- memset(&ofn, 0, sizeof(ofn));
- TDocTemplate* ptpl;
- TProcInstance TplHookProcInstance((FARPROC)TplHook);
-
- // Concatenate description and filters into a single string
- //
- for (len=2, count=0; count < tplcount; count++) {
- ptpl = tpllist[count];
- len += (strlen(ptpl->GetFileFilter())+2); // space for two null separators
- if (ptpl->GetDescription())
- len += strlen(ptpl->GetDescription());
- }
- filtbuf = new char[len];
- for (pbuf = filtbuf, count=0; count < tplcount; count++) {
- ptpl = tpllist[count];
- if ((len = strlen(ptpl->GetDescription())) != 0) {
- strcpy(pbuf, ptpl->GetDescription());
- pbuf += len + 1;
- }
- else {
- *pbuf++ = 0;
- }
- strcpy(pbuf, ptpl->GetFileFilter());
- pbuf += strlen(pbuf) + 1;
- }
- *pbuf = 0; // double null to signify end
-
- // set selection to previously selected template if present
- //
- index=tplcount-1;
-
- struct TplHookData hookData = {flags,&ofn.Flags,extbuf,tpllist,index,save};
- ofn.nFilterIndex = index + 1; // Note: Windows *might* decrement this by one
- ofn.lpstrInitialDir = tpllist[index]->GetDirectory();
- ofn.lpstrDefExt = (LPCSTR)&extbuf; // receives string from hook function
- ofn.lStructSize = sizeof(OPENFILENAME);
- ofn.hwndOwner = GetApplication()->GetMainWindow()->HWindow;
- ofn.lpstrFilter = filtbuf;
- ofn.lpstrFile = path;
- ofn.nMaxFile = buflen;
- ofn.Flags = OFN_ENABLEHOOK;
- ofn.lCustData = (LPARAM)&hookData;
- (DLGPROC)ofn.lpfnHook = (DLGPROC)(FARPROC)TplHookProcInstance;
-
- GetApplication()->EnableCtl3dAutosubclass(true);
-
- if ((save ? ::GetSaveFileName(&ofn) : ::GetOpenFileName(&ofn)) == 0) {
- uint32 err = ::CommDlgExtendedError(); // temp for debugging use
- if (err != 0)
- TRACEX(OwlDocView, 0, "OpenFileName error:" << err);
- ofn.nFilterIndex = 0;
- }
- else
- {
- // Set flag to remember template which was selected
- //
- for (index=(int)ofn.nFilterIndex-1, count=0; count < tplcount;
- count++) {
- if (count == index)
- tpllist[count]->SetFlag(dtSelected);
- else
- tpllist[count]->ClearFlag(dtSelected);
- }
- // Update template with directory from dialog if flag set
- //
- ptpl = tpllist[(int)ofn.nFilterIndex - 1];
- if (ofn.nFileOffset && (ptpl->IsFlagSet(dtUpdateDir))
- &&
- (ptpl->GetDirectory()==0
- || memcmp(ptpl->GetDirectory(),
- ofn.lpstrFile,ofn.nFileOffset) != 0))
- ptpl->SetDirectory(ofn.lpstrFile, ofn.nFileOffset);
- }
- delete [] filtbuf;
- GetApplication()->EnableCtl3dAutosubclass(false);
- return (int)ofn.nFilterIndex;
- }
-
-
- TDocTemplate* MWDocManager::SelectAnySave (TDocument& doc, bool)
- {
- // TDocTemplate* result;
-
- // result = TDocManager::SelectAnySave(doc, samedoc);
-
- // INSERT>> Your code here.
- TDocTemplate* ptpl;
- char filepath[256];
- int index;
- TDocTemplate* tpllist[2];
- tpllist[0] = doc.GetTemplate();
- int tplcount=1;
-
- if (doc.GetDocPath())
- strcpy(filepath, doc.GetDocPath());
- else
- filepath[0] = 0; // no initial file path
- index = SelectDocPath(tpllist, tplcount, filepath, sizeof(filepath),0,true);
- if (!index)
- return 0; // user cancel or dialog error
- ptpl = tpllist[index-1];
- if (!doc.SetDocPath(filepath))
- return 0;
- return ptpl;
-
- // return result;
- }
- --------------------------------------------------------
-
- don't forget to call
- SetDocManager(new MWDocManager(dmMDI, this));
- in your TApplication constructor
-
- Hope this helps somebody
-
- Agrivar
-
-
- Agrivar
-
- aka Randy Charles Morin
- MiddleWorld SoftWare
- Canada: 1-800-363-3780
- Other: 905-279-2087
-